package com.attendance.www.hr.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.attendance.www.hr.entitys.Employee;

/**
 * 
 * @author 递归遍历查询N+1、N+2
 *
 */
public class FactorialUtils {

	/**
	 * 用于存储N+1、N+2结果存储
	 */
	private static List<String> employeeCodeList=null;
	
	/**
	 * 用于存储N+1、N+2结果存储
	 */
	private static List<Integer> employeeIdList=null;
	
	/**
	 * 过滤重复人员ID
	 * @param eIdList
	 * @return
	 */
	public static List<Integer> filterEmployeeId(List<Integer> eIdList){
		employeeIdList=new ArrayList<Integer>();
		for(int i=0;i<eIdList.size();i++) {
			if(isEmployeeIdList(eIdList.get(i))) {
				//保存结果
				employeeIdList.add(eIdList.get(i));
			}
		}
		return employeeIdList;
	}
	
	
	/**
	 * 递归遍历-返回员工号集合
	 * @param employeeCode
	 * @param employeeList
	 * @return
	 */
	public static List<String> factorialEmployeeCode(String employeeCode,List<Employee> employeeList) {
		employeeCodeList=new ArrayList<String>();
		employeeCodeList.add(employeeCode);
		//递归查询N+1结果
		factorialN1empCode(employeeCode,employeeList,true);
		//递归查询N+2结果
		factorialN2empCode(employeeCode,employeeList,true);
		return employeeCodeList;
	}
	
	/**
	 * 递归遍历-返回员工号集合
	 * @param employeeCode
	 * @param employeeList
	 * @return
	 */
	public static List<Integer> factorialEmployeeId(Integer employeeId,List<Employee> employeeList) {
		employeeIdList=new ArrayList<Integer>();
		//employeeIdList.add(employeeId);
		String employeeCode=null;
		//遍历回去员工号
		for(int i=0;i<employeeList.size();i++) {
			if(employeeList.get(i).getEmployeeId().equals(employeeId)) {
				employeeCode=employeeList.get(i).getEmployeeNo();
				break;
			}
		}
		//递归查询N+1结果
		factorialN1empCode(employeeCode,employeeList,false);
		//递归查询N+2结果
		factorialN2empCode(employeeCode,employeeList,false);
		return employeeIdList;
	}

	/**
	 * 递归N+1
	 * @param employeeCode
	 * @param employeeList
	 * @param type true 员工号、false 员工ID
	 */
	private static void factorialN1empCode(String employeeCode,List<Employee> employeeList,boolean type) {
		Iterator<Employee> iterator = employeeList.iterator();
		while(iterator.hasNext()){
			Employee employee = (Employee)iterator.next();
        	//N+1
			String n1empCode=employee.getN1empCode();
			//员工号
			String employeeNo=employee.getEmployeeNo();
			//员工id
			Integer employeeId=employee.getEmployeeId();
			
		    if(StringUtils.isNotBlank(n1empCode)) {
		    	if(n1empCode.equals(employeeCode)) {
		    		if(type) {
		    			//将结果存在返回值
			    		if(isEmployeeCodeList(employeeNo)) {
			    			//保存结果
			    			employeeCodeList.add(employeeNo);
			    		}
		    		}else {
		    			//将结果存在返回值
		    			if(isEmployeeIdList(employeeId)) {
			    			//保存结果
		    				employeeIdList.add(employeeId);
			    		}
		    		}
		    		//递归员工号，查询下一层
		    		factorialN1empCode(employeeNo,employeeList,type);
		    	}
		    }
        }
	}
	
	/**
	 * 递归N+2
	 * @param employeeCode
	 * @param employeeList
	 * @param type true 员工号、false 员工ID
	 */
	private static void factorialN2empCode(String employeeCode,List<Employee> employeeList,boolean type) {
		Iterator<Employee> iterator = employeeList.iterator();
        while(iterator.hasNext()){
        	Employee employee = iterator.next();
        	//N+2
			String n2empCode=employee.getN2empCode();
			//员工号
			String employeeNo=employee.getEmployeeNo();
			//员工id
			Integer employeeId=employee.getEmployeeId();
			
		    if(StringUtils.isNotBlank(n2empCode)) {
		    	if(n2empCode.equals(employeeCode)) {
		    		if(type) {
		    			//将结果存在返回值
			    		if(isEmployeeCodeList(employeeNo)) {
			    			//保存结果
			    			employeeCodeList.add(employeeNo);
			    		}
		    		}else {
		    			//将结果存在返回值
		    			if(isEmployeeIdList(employeeId)) {
			    			//保存结果
		    				employeeIdList.add(employeeId);
			    		}
		    		}
		    		//递归员工号，查询下一层
		    		factorialN2empCode(employeeNo,employeeList,type);
		    	}
		    }
        }
	}
	
	/**
	 * 判断集合是否存在员工号
	 * @param employeeNo
	 * @return
	 */
	private static boolean isEmployeeCodeList(String employeeNo) {
		boolean type=true;
		for(int i=0;i<employeeCodeList.size();i++) {
			if(employeeCodeList.get(i).equals(employeeNo)) {
				type=false;
				break;
			}
		}
		return type;
	}
	
	/**
	 * 判断集合是否存在员工号
	 * @param employeeNo
	 * @return
	 */
	private static boolean isEmployeeIdList(Integer employeeId) {
		boolean type=true;
		for(int i=0;i<employeeIdList.size();i++) {
			if(employeeIdList.get(i).equals(employeeId)) {
				type=false;
				break;
			}
		}
		return type;
	}
}
